import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // 存储token值
    token: '',
    userinfo: ''
  },
  mutations: {
    // 需要更改token值
    updataToken (state, token) {
      state.token = token
    },
    // 获取用户信息
    UserInfo (state, info) {
      state.userinfo = info
    }
  },
  actions: {
    // 获取数据  定义初始化用户基本信息 action函数
    async userInfoToken (context) {
      const res = await axios.get('/my/userinfo', {
        headers: {
          Authorization: context.state.token
        }
      })
      if (res.data.code === 0) {
        // console.log(res.data.data)
        context.commit('UserInfo', res.data.data)
      }
    }
  },
  modules: {
  }
})
